package com.mls.guardian.appst;

import com.mls.guardian.appst.chain.TaskResult;
import com.mls.guardian.appst.service.ProjectService;
import com.mls.guardian.appst.service.RestartAppService;
import com.mls.guardian.config.AppSTConfig;
import com.mls.guardian.config.ProjectConfig;
import com.mls.guardian.config.VersionConfig;
import com.mls.guardian.dataobj.BackupProjectObj;
import com.mls.guardian.dataobj.FileObj;
import com.mls.guardian.dataobj.ProcessMessage;
import com.mls.guardian.dataobj.ServerProcessInfo;
import com.mls.guardian.em.ContainerRole;
import com.mls.guardian.protocol.body.AppRestartBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jl on 15/10/21.
 */
public class AppController {
    private static final Logger log = LoggerFactory.getLogger(AppController.class);
    private List<ProjectConfig> projectObjList = new ArrayList();

    private AppSTConfig appSTConfig;
    private RestartAppService restartAppService;
    private ProjectService projectService;


    public AppController(AppSTConfig appSTConfig) {
        this.appSTConfig = appSTConfig;
        restartAppService = new RestartAppService(this, appSTConfig);
        projectService = new ProjectService(this, appSTConfig);

    }


    public void startup(List<ProjectConfig> projectObjList) throws Exception {
        this.projectObjList = projectObjList;

        /*重启moudle过程
        1.清理环境
        2.备份源程序
        3.替换config.js文件
        4.编译
        5.移动生成lib到执行目录
        6.重启
         */
        restartAppService.init(projectObjList);
        projectService.init(projectObjList);
        log.info("appcontroller started ok");
    }


    public void setProjectObjList(List<ProjectConfig> projectObjList) {
        this.projectObjList = projectObjList;
    }


    public AppRestartBody startApp(String projectName, List<ProcessMessage> list) throws Exception {
        return restartAppService.startApp(projectName, list);
    }

    public AppRestartBody restartApp(String projectName, List<ProcessMessage> list, String versionTag) throws Exception {
        return restartAppService.restartApp(projectName, list, versionTag);
    }

    public AppRestartBody stopApp(String projectName, List<ProcessMessage> list) throws Exception {
        return restartAppService.stopApp(projectName, list);
    }

    public AppRestartBody updateStaticFile(String projectName, List<ProcessMessage> list) throws Exception {
        return restartAppService.updateStaticFile(projectName, list);
    }

    public List<BackupProjectObj> getBackup(String projectName, String versionTag) throws Exception {
        return restartAppService.getBackup(projectName, versionTag);
    }

    public AppRestartBody restoreApp(String projectName, String versionTag) throws Exception {
        return restartAppService.restoreApp(projectName, versionTag);
    }


    public ServerProcessInfo getProjectAndStatus(String projectName, ContainerRole role) throws Exception {
        return projectService.getProjectAndStatus(projectName, role);
    }

    /**
     * 获取指定目录下面的子文件
     *
     * @param projectName WMS
     * @param moduleName  BaseComServer
     * @param parent      a/b/c
     * @return
     */
    public List<FileObj> getFileInfo(String projectName, String moduleName, String... parent) throws Exception {
        return this.projectService.getFileInfo(projectName, moduleName, parent);
    }

    public TaskResult upgrade(ContainerRole role, String upgradeVersion) throws Exception {
        return this.restartAppService.upgrade(role, upgradeVersion);
    }

    public String getUpgradeVersion() {
        return this.projectService.getUpgradeVersion();
    }

}
